﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Classified_UserControls_Cld_List : System.Web.UI.UserControl
{
     
    RegistrationData _objRegistrationData = new RegistrationData();
    DAL.AdDB _objAdDB = new DAL.AdDB();
    const int PageSize = 5;
    int PagingNumsize = 4;
    public string TotalAds = string.Empty, imagepath = string.Empty, desc = string.Empty;
    public DataSet dsAds { get; set; }

    public delegate DataSet ReBindDataSet(object sender);
    public event ReBindDataSet RequestDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!object.Equals(Session["User"], null))
        {
            _objRegistrationData = Session["User"] as RegistrationData;
        }
        if (!IsPostBack)
        {
            BindAdList();
            ViewState["Pg"] = 1;
        }

        else
        {
            if (Request.Form["__EVENTTARGET"].ToLower() == "paging")
            {
                if (Request.Form["__EVENTARGUMENT"] != null)
                {
                    string args = Request.Form["__EVENTARGUMENT"].ToString().ToLower();
                    int num = 0;
                    if (Int32.TryParse(args, out num))
                    {
                        ViewState["Pg"] = num;
                        Paging(PageSize, num);
                    }
                    else if (args == "next")
                    {
                        int viewPG = Convert.ToInt32(ViewState["Pg"]);
                        ViewState["Pg"] = viewPG + 1;
                        Paging(PageSize, viewPG + 1);
                    }
                    else if (args == "previous")
                    {
                        int viewPG = Convert.ToInt32(ViewState["Pg"]);
                        ViewState["Pg"] = viewPG - 1;
                        Paging(PageSize, viewPG - 1);
                    }
                }
            }
        }
    }

    protected void BindAdList()
    {
            try
            {
                if (dsAds != null)
                {
                    if (dsAds.Tables.Count > 0)
                    {
                        if (dsAds.Tables[0].Rows.Count > 0)
                        {
                            Session["dsAds"] = dsAds;

                            Paging(PageSize, 1);
                            ViewState["TotalAds"] = dsAds.Tables[0].Rows.Count.ToString();
                        }
                        else
                            divEmptyMessage.Style.Value = "display:block";
                    }
                    else
                        divEmptyMessage.Style.Value = "display:block";
                }
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message;
            }
        
    }

    public void imgDelete_Click(object sender, ImageClickEventArgs args)
    {
        ImageButton b = sender as ImageButton;
        RepeaterItem ri = b.Parent as RepeaterItem;
        Label lblAdID = ri.FindControl("lblAdID") as Label;

        if (!object.Equals(Session["User"], null))
        {
            _objRegistrationData = Session["User"] as RegistrationData;
            _objAdDB.Activate_Deactivate_Ad(Convert.ToInt32(lblAdID.Text), _objRegistrationData.UserID, false);
           
        }
        this.dsAds = RequestDataSet(sender);
        BindAdList();
    }


    public void imgAcitvate_Click(object sender, ImageClickEventArgs args)
    {
        ImageButton b = sender as ImageButton;
        RepeaterItem ri = b.Parent as RepeaterItem;
        Label lblAdID = ri.FindControl("lblAdID") as Label; 

        if (!object.Equals(Session["User"], null))
        {
            _objRegistrationData = Session["User"] as RegistrationData;
            _objAdDB.Activate_Deactivate_Ad(Convert.ToInt32(lblAdID.Text), _objRegistrationData.UserID, true);

        }
        this.dsAds = RequestDataSet(sender);
        BindAdList();
    }

    protected void rptAdList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lbl = ((Label)e.Item.FindControl("lblFeaturedAdID"));
            if (lbl.Text == "1")
            {
                ((HtmlGenericControl)(e.Item.FindControl("divFeatured"))).Attributes["style"] = "visibility:visible;";
                ((HtmlGenericControl)(e.Item.FindControl("divFeaturedImg"))).Attributes["style"] = "visibility:visible;";
            }
            else
            {
                ((HtmlGenericControl)(e.Item.FindControl("divFeatured"))).Attributes["style"] = "visibility:hidden;";
                ((HtmlGenericControl)(e.Item.FindControl("divFeaturedImg"))).Attributes["style"] = "visibility:hidden;";
            }
            Image imgPath = ((Image)e.Item.FindControl("imgAdImage"));
            if(!string.IsNullOrEmpty( imgPath.ImageUrl ))
                 imgPath.ImageUrl="~/Classified/" + imgPath.ImageUrl;
            else
                imgPath.ImageUrl = "~/Classified/images/no-image.jpg";

            Label lblPostedBy = ((Label)e.Item.FindControl("lblPostedBy"));
            if (Convert.ToInt32(lblPostedBy.Text) == _objRegistrationData.UserID && _objRegistrationData.UserID>0)
            {
                Label lblIsActive = ((Label)e.Item.FindControl("lblIsActive"));
                if (Convert.ToBoolean(lblIsActive.Text))
                {
                    ((ImageButton)e.Item.FindControl("imgDelete")).Visible = true;
                    ((ImageButton)e.Item.FindControl("imgActivate")).Visible = false;
                }
                else
                {
                    ((ImageButton)e.Item.FindControl("imgDelete")).Visible = false;
                    ((ImageButton)e.Item.FindControl("imgActivate")).Visible = true;
                }
                ((Image)e.Item.FindControl("imgEdit")).Visible = true;
            }
            
        }
    }


    DataSet dsPaging = new DataSet();

    public void Paging(int size, int pageNum)
    {
        int p = pageNum * size;
        int s = p - size;
        DataSet dsFilterPage = new DataSet();
        dsPaging = Session["dsAds"] as DataSet;
        dsFilterPage = dsPaging.Clone();

        // get total no of rows in label
        int totalPage = dsPaging.Tables[0].Rows.Count / size;
        int inc = dsPaging.Tables[0].Rows.Count % size;
        if (inc > 0)
        {
            totalPage = totalPage + 1;
        }

        if (pageNum < 1)
        {
            ViewState["Pg"] = 1;
        }
        if (pageNum > totalPage)
        {
            ViewState["Pg"] = totalPage;
        }

        if (pageNum <= totalPage && pageNum >= 1)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (pageNum <= 1)
                sb.Append("  <li><a href='javascript:void();' id='btn' >Previous</a></li>");
            else
                sb.Append("  <li><a href=javascript:__doPostBack('paging','previous') >Previous</a></li>");

            int halfpageNumSize = PagingNumsize / 2;
            if (totalPage < PagingNumsize)
            {
                PagingNumsize = totalPage;
            }

            if (halfpageNumSize < pageNum)
            {
                int startPage = pageNum - halfpageNumSize;
                if (startPage + PagingNumsize > totalPage)
                {
                    startPage = totalPage - PagingNumsize;
                }

                for (int i = startPage + 1; i <= startPage + PagingNumsize; i++)
                {
                    if (i == pageNum)
                        sb.Append("  <li><a href='javascript:void();' class='selected'>" + i + "</a></li>");
                    else
                        sb.Append("  <li><a href=javascript:__doPostBack('paging','" + i + "')>" + i + "</a></li>");
                }
            }
            else
            {
                for (int i = 1; i <= PagingNumsize; i++)
                {
                    if (i == pageNum)
                        sb.Append("  <li><a href='javascript:void();' class='selected'>" + i + "</a></li>");
                    else
                        sb.Append("  <li><a href=javascript:__doPostBack('paging','" + i + "')>" + i + "</a></li>");
                }
            }
            if (pageNum == totalPage)
                sb.Append("  <li><a href='javascript:void();'  id='btn' >Next</a></li>");
            else
                sb.Append("  <li><a href=javascript:__doPostBack('paging','next') >Next</a></li>");
            ulPageing.InnerHtml = sb.ToString();
            ulBottomPaging.InnerHtml = sb.ToString();

            for (int i = s; i < p; i++)
            {
                if (s < dsPaging.Tables[0].Rows.Count)
                {
                    //    DataRow dr = dsFilterPage.Tables[0].NewRow();
                    DataRow drNew = dsPaging.Tables[0].Rows[s];
                    dsFilterPage.Tables[0].ImportRow(drNew);
                    s++;
                }
                else
                {
                    break;
                }
            }

            rptAdList.DataSource = dsFilterPage.Tables[0];
            rptAdList.DataBind();
        }

    }
}